package
{
	import com.adobe.webapis.flickr.FlickrService;
	import com.adobe.webapis.flickr.Photo;
	import com.adobe.webapis.flickr.events.FlickrResultEvent;
	import com.adobe.webapis.flickr.methodgroups.Interestingness;
	import com.adobe.webapis.flickr.methodgroups.Photos;
	import com.jxl.coreplay.core.AbstractApplication;
	import com.jxl.coreplay.flash.Slideshow;
	import com.jxl.coreplay.vo.SlideshowImageVO;
	
	import fl.data.DataProvider;
	
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.FullScreenEvent;

	public class Testing_FlickrShow extends AbstractApplication
	{
		public static const POSITION_CENTER:String 			= "center";
		public static const POSITION_EXACT_FIT:String 		= "exactfit";
		
		public var slideshow:Slideshow;
		public var slideshowWidth:Number 					= 480;
		public var slideshowHeight:Number 					= 325;
		public var position:String							= "center";
		public var images:DataProvider;
		public var service:FlickrService;
		public var photos:Photos;
		public var interesting:Interestingness;
		
		public function Testing_FlickrShow()
		{
			super();
		}
		
		protected override function init():void
		{
			super.init();
			
			//images.addItem(new SlideshowImageVO("http://farm3.static.flickr.com/2169/2211715946_b3ae779d67_b.jpg"));
			
			if(this.loaderInfo.parameters.position == POSITION_CENTER)
			{
				// nothing
			}
			else if(this.loaderInfo.parameters.position == POSITION_EXACT_FIT)
			{
				position = POSITION_EXACT_FIT;
			}
			
			slideshow = new Slideshow();
			addChild(slideshow);
			
			resize();
			
			service 			= new FlickrService("bb19f82cfca36183ccc1b1af0467cfac");
			service.secret 		= "0a01d253e6317df6";
			service.addEventListener(FlickrResultEvent.PHOTOS_SEARCH, onSearchResult);
			service.addEventListener(FlickrResultEvent.INTERESTINGNESS_GET_LIST, onSearchResult);
			
			var dec:Date = new Date(2007, 12, 1);
			
			photos = new Photos(service);
			photos.search("", "rave,dance,club", "all", "", null, null, null, null, -1, "", 100, 1, "interestingness-desc");
			
			
			interesting = new Interestingness(service);
			//interesting.getList(null, "rave,dance", 16);
		}
		
		protected function onSearchResult(event:FlickrResultEvent):void
		{
			var len:int = event.data.photos.photos.length;
			var url:String;
			var photo:Photo;
			images = new DataProvider();
			for (var a:int = 0; a < len; a++)
			{
				photo 	= event.data.photos.photos[a] as Photo;
				//http://farm3.static.flickr.com/2169/2211715946_b3ae779d67_t.jpg
				url		= "http://farm" + photo.farmId + ".static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + ".jpg";
				images.addItem(new SlideshowImageVO(url));
			}
			slideshow.dataProvider = images;
		}
		
		protected override function onFullScreen(event:FullScreenEvent):void
		{
			resize(event.fullScreen);
		}
		
		protected override function onResize(event:Event):void
		{
			resize(stage.displayState == StageDisplayState.FULL_SCREEN);
		}
		
		protected function resize(fullscreen:Boolean=false):void
		{
			if(slideshow == null) return;
			if(fullscreen == false)
			{
				if(position == POSITION_CENTER)
				{
					slideshow.setSize(slideshowWidth, slideshowHeight);
					slideshow.move((stage.stageWidth / 2) - (slideshow.width / 2),
									(stage.stageHeight / 2) - (slideshow.height / 2));
				}
				else
				{
					slideshow.move(0, 0);
					slideshow.setSize(stage.stageWidth, stage.stageHeight);
				}
			}
			else
			{
				slideshow.move(0, 0);
				slideshow.setSize(stage.stageWidth, stage.stageHeight);
			}
			slideshow.validateNow();
		}
	}
}